package listbyorder.access201_300.test279;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/27 12:24
 */
public class Solution1 {

    int res = Integer.MAX_VALUE;

    public int numSquares(int n) {
        int len = (int) Math.sqrt(n);
        List<Integer> choice = new ArrayList<>();
        for (int i = 1; i <= len; i++) {
            choice.add(0, i * i);
        }
        helper(choice, n, 0, new ArrayList<Integer>());
        return res;
    }

    private void helper(List<Integer> choice, int n, int start, List<Integer> path) {
        if (n == 0) {
            res = Math.min(res, path.size());
            return;
        }
        if (n < 0) {
            return;
        }
        for (int i = start; i < choice.size(); i++) {
            path.add(choice.get(i));
            helper(choice, n - choice.get(i), i, path);
            path.remove(path.size() - 1);
        }
    }

}
